/**
 * 上传控件 依赖文件： jquery/jquery-1.11.2.min.js meeting/meeting.core.js
 *
 */
(function (mt) {
    /**
     * 自定义输入项
     * @param id 输入项信息Id（小于1新增，大于0修改）
     * @param callback 回调函数，返回保存后的输入项信息
     * @param option 配置信息
     */
    mt.customInput = function (id, callback, option) {
        var defaultOption = {
            organizeId: -1,
            useId: -1
        }

        $.extend(true, defaultOption, option);

        var url = mt.webSite.fullhost() + "/input/dialog/edit"
        if (id) {
            url += "?id=" + id;
        }
        url += (id ? "&" : "?") + "organizeId=" + defaultOption.organizeId;
        url += "&useId=" + defaultOption.useId;

        window.parent.jQuery.confirm({
            title: '输入项信息',
            content: 'url:' + url,
            columnClass: 'medium',
            buttons: {
                save: {
                    text: "保存",
                    action: function () {
                        var curdialog = this.$content.find("#inputinfoform");
                        //将标识值设置为和名称一样
                        curdialog.find("input[name='mark']").val(curdialog.find("input[name='caption']").val());
                        var iscancel = true;
                        $.ajax({
                            async: false,
                            url: mt.webSite.fullhost() + '/input/dialog/save',
                            data: curdialog.serialize(),
                            success: function (result) {
                                result = JSON.parse(result);
                                if (result.state > 0) {
                                    //保存后将Id返填到界面
                                    curdialog.find("input[name='id']").val(result.data.id);
                                    //执行回调方法
                                    if ($.isFunction(callback)) {
                                        var callbackresult = callback(result.data);
                                        if (callbackresult != undefined && callbackresult != "undefined") {
                                            iscancel = Boolean(callbackresult);
                                        }
                                    }
                                } else {
                                    window.parent.jQuery.alert("保存失败");
                                    iscancel = false;
                                }
                            }, error: function (x) {
                                console.log(x);
                            }
                        });

                        if (iscancel) {
                            window.parent.jQuery.alert("保存成功");
                        }

                        return iscancel;
                    }
                },
                cancel: {
                    text: "取消"
                }
            }
        });
    }

    mt.customInput.form = {
        /**
         * 加载输入项控件
         * @param el 目标元素（jquery选择器）
         * @param ids 输入项信息id列表
         * @param option 配置
         * @param callback 加载完回调函数
         */
        load: function (el, ids, option, callback) {
            var defaultOption = {
                col: 1
            }

            $.extend(true, defaultOption, option);

            $.ajax({
                url: mt.webSite.fullhost() + '/input/dialog/search',
                data: {
                    ids: ids
                },
                success: function (result) {
                    $(el).html(loadhtml(JSON.parse(result), defaultOption.col));
                    if ($.isFunction(callback)) {
                        callback();
                    }
                },
                error: function (x) {
                    console.log(x);
                }
            })
        },
        serialize: function (el) {
            var data = [];
            $(el).find(".mtcol").each(function () {
                var inputinfoconfig = $(this).find("label");
                var id = inputinfoconfig.attr("inputid");
                var caption = inputinfoconfig.text();
                var mask = inputinfoconfig.attr("mask");
                var type = inputinfoconfig.attr("controltype");
                var val = $(this).find("input,textarea,select").val();
                data.push({id: id, caption: caption, mask: mask, type: type, val: val});
            });

            //更新自定义批号
            $("select[controltype='batch'] option:selected,select[controltype='批号'] option:selected").each(function () {
                meeting.customcode.getCurCodeByCode($(this).attr("code"), 1);
            })
            return data;
        }
    }

    /**
     * 创建输入项控件html
     * @param inputinfolist 输入项信息
     * @param col 按多少列排列
     * @returns {string}
     */
    function loadhtml(inputinfolist, col) {
        var start = 0;
        var html = "";
        for (var i = 0; i < inputinfolist.length; i++) {
            var inputinfo = inputinfolist[i];
            if (start == 0) {
                //增加一行
                html += "<div class='mtrow'>";
            }
            start++;

            html += "<div class='mtcol'>";
            html += "<label inputid='" + inputinfo.id + "'  controltype='" + inputinfo.typeName + "' mask='" + inputinfo.mark + "' class='mtlabel' for='" + inputinfo.mark + "'>" + inputinfo.caption + " </label>";
            switch (inputinfo.typeName) {
                case "text":
                case "文本":
                    html += "<input type='text' class='mtinput' " + (inputinfo.isRequire == 1 ? "required" : "") + " inputid='" + inputinfo.id + "' name='ci" + inputinfo.id + "' value='" + inputinfo.defaultVal + "' />";
                    break;
                case  "number":
                case "数字":
                    html += "<input type='text' class='mtinput' " + (inputinfo.isRequire == 1 ? "required" : "") + " inputid='" + inputinfo.id + "'  name='ci" + inputinfo.id + "' value='" + inputinfo.defaultVal + "' />";
                    break;
                case  "select":
                case "选择":
                    var selecthtml = "<select class='mtinput' " + (inputinfo.isRequire == 1 ? "required" : "") + " inputid='" + inputinfo.id + "'  name='ci" + inputinfo.id + "' value='" + inputinfo.defaultVal + "'>";
                    for (var j = 0; j < inputinfo.itemList.length; j++) {
                        var inputItem = inputinfo.itemList[j];
                        selecthtml += "<option " + (inputItem.val == inputinfo.defaultVal ? "selected='true'" : "");
                        selecthtml += " value='" + inputItem.val + "'>" + inputItem.caption + "</option>";
                    }
                    selecthtml += "</select>";
                    html += selecthtml;
                    break;
                case "batch":
                case "批号":
                    var batchhtml = "<select controltype='" + inputinfo.typeName + "' class='mtinput' " + (inputinfo.isRequire == 1 ? "required" : "") + " inputid='" + inputinfo.id + "'  name='ci" + inputinfo.id + "' value='" + inputinfo.defaultVal + "'>";
                    for (var j = 0; j < inputinfo.itemList.length; j++) {
                        var inputItem = inputinfo.itemList[j];
                        var curval = meeting.customcode.getCurCodeByCode(inputItem.val);
                        batchhtml += "<option code='" + inputItem.val + "'  value='" + curval + "'>" + curval + "</option>";
                    }
                    batchhtml += "</select>";
                    html += batchhtml;
                    break;
                case  "date":
                case "日期":
                    html += "<input type='text' class='mtinput' " + (inputinfo.isRequire == 1 ? "required" : "") + " inputid='" + inputinfo.id + "'  name='" + inputinfo.mark + "' value='" + inputinfo.defaultVal + "' onclick='WdatePicker({dateFmt:\"yyyy-MM-dd\"})'/>";
                    break;
            }

            html += "</div>";

            if (start == col || i == inputinfolist.length - 1) {
                start = 0;
                //行结尾
                html += "</div>";
            }
        }

        return html;
    }

}(meeting));